package com.hlhj.funlike_business.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hlhj.funlike_business.R;
import com.hlhj.funlike_business.activity.adapter.WaterListAdp;
import com.hlhj.funlike_business.bean.TotalBean;
import com.hlhj.funlike_business.bean.WaterListBean;
import com.hlhj.funlike_business.presenter.CashierPresenter;
import com.hlhj.funlike_business.views.CashierView;
import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class CashierAty extends BaseActivity<CashierView,CashierPresenter> implements CashierView {
    private ImageView img_back;
    private TextView tv_leijishouyi_money,tv_month,tv_week,tv_day;
    private RecyclerView recycle_list;
    private SpringView spring;
    private WaterListAdp waterListAdp;
    private int page=1;
    private List<WaterListBean.DataBeanX.DataBean> dataBeanList;

    @Override
    protected int getContentId() {
        return R.layout.aty_cashier;
    }

    @Override
    protected void initData() {
        dataBeanList=new ArrayList<>();
        waterListAdp=new WaterListAdp(dataBeanList);
        recycle_list.setLayoutManager(new LinearLayoutManager(this){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        recycle_list.setAdapter(waterListAdp);

        spring.setHeader(new DefaultHeader(this));
        spring.setFooter(new DefaultFooter(this));
    }

    @NotNull
    @Override
    public CashierView bindView() {
        return this;
    }

    @NotNull
    @Override
    public CashierPresenter createPresenter() {
        return new CashierPresenter();
    }

    @Override
    public void initView() {
        img_back = findViewById(R.id.img_back);
        tv_leijishouyi_money = findViewById(R.id.tv_leijishouyi_money);
        tv_month = findViewById(R.id.tv_month);
        tv_week = findViewById(R.id.tv_week);
        tv_day = findViewById(R.id.tv_day);
        recycle_list = findViewById(R.id.recycle_list);
        spring = findViewById(R.id.spring);
    }

    @Override
    public void logicStart() {
        getPresenter().LoadTotalMoney(this);
        getPresenter().LoadWaterList(this,page);
    }

    @Override
    public void setListener() {
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        spring.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                page=1;
                getPresenter().LoadWaterList(CashierAty.this,page);
                spring.onFinishFreshAndLoad();
            }

            @Override
            public void onLoadmore() {
                page++;
                getPresenter().LoadWaterList(CashierAty.this,page);
                spring.onFinishFreshAndLoad();
            }
        });
    }

    @Override
    public void showMoney(TotalBean.DataBean dataBean) {
        tv_leijishouyi_money.setText("￥" + dataBean.getTotal());
        tv_month.setText("￥" + dataBean.getMouthMoney());
        tv_week.setText("￥" + dataBean.getWeekMoney());
        tv_day.setText("￥" + dataBean.getDayMoney());
    }

    @Override
    public void showWaterList(List<WaterListBean.DataBeanX.DataBean> dataBeans) {
        if (page==1){
            dataBeanList.clear();
            dataBeanList.addAll(dataBeans);
        }else {
            dataBeanList.addAll(dataBeans);
        }
        waterListAdp.notifyDataSetChanged();
    }
}
